//保存表单
function saveForm(form) {
	var v = jQuery(form).validate({
		messages: fieldMsg,
		submitHandler: function() {
			//获取表单数据
			var data = '';
			jQuery(document.adminForm).find(':text,:password,:radio:checked,:checkbox:checked,select,textarea,:hidden').each(function(i){
			if (typeof this.name!='undefined' && typeof this.value != 'undefined')
				data += '&' + this.name + '=' + encodeURIComponent(this.value);
			});
			data = data.substr(1);

			//表单action
			var action = document.adminForm.action || location.href;

			//表单method
			var method = 'POST';

			//发送AJAX请求
			jQuery.ajax({
				type: method,
				url: action,
				data: data,
				dataType: "json",
				success: function(res) {
					if (res == 1) window.top.setTimeout('window.top.document.getElementById(\'sbox-window\').close();window.top.location=window.top.location', 700);
					else if (res) showErrorMsg(res);
				}
			});				 
		 }
	 });
	return false;
} 

function showErrorMsg(errorJson)
{
	for(var e in errorJson) {
		if (jQuery('#' + e).next('label').size())
			jQuery('#' + e).next('label').html(errorJson[e]).show();
		else jQuery('#' + e).parent().append('<label for="' + e + '" generated="true" class="error">' + errorJson[e] + '</label>');
	}
}

function copyHTML(contentId) {
      
	jQuery("#"+contentId+"-wrap").append("<div class='" + contentId + "'>" + jQuery("."+ contentId)[0].innerHTML + '<a href="###" class="left" onclick="cancelHTML(this);return false;" title="删除添加"><img src="' + _root + 'images/delete.png" /></a>');
	jQuery('.'+ contentId).last().find("input")[0].id = contentId + "_date_" + (jQuery('.'+ contentId).size()-1);
	jQuery('.'+ contentId).last().find("tbody")[0].id = contentId + "manureDate_table_" + (jQuery('.'+ contentId).size()-1);
	jQuery('.'+ contentId).last().find("tbody")[0].find("tr")[0].id = contentId + "manureDate_table_" + (jQuery('.'+ contentId).size()-1);
	jQuery('.'+ contentId).last().find("img")[0].id = contentId + "_date_" + (jQuery('.'+ contentId).size()-1)+"_img";
	var inputField = contentId + "_date_" + (jQuery('.'+ contentId).size()-1);
	var buttonImg =  contentId + "_date_" + (jQuery('.'+ contentId).size()-1) + "_img";
	window.addEvent('domready', function() {
		Calendar.setup({
			inputField : inputField, // id of the input field
			ifFormat : "%Y-%m-%d", // format of the input field
			button : buttonImg, // trigger for the calendar (button ID)
			align : "Tl", // alignment (defaults to "Bl")
			singleClick : true
		});
	});
    
    jQuery("#"+contentId+"-wrap").append('<div style="clear:both;"></div>');
}

function cancelHTML(obj) {
	jQuery(obj).parent().remove();
}

jQuery(function(){
	jQuery('#ppid').change(function(){
		var v = this.value;
		if (v == 0) {
			jQuery('.hideinfo').hide();
			jQuery('#opes').html('');
		} else {			
			jQuery.ajax({
				type: "POST",
				url: "?option=com_farm&controller=operation&task=getFieldAndPlantInfo&ppid=" + v,
				dataType: "json",
				success: function(res) {
					if (!res) return;
					
					jQuery('#fid').html(res.ppinfo.field);
					jQuery('#pid').html(res.ppinfo.plant);
					jQuery('.hideinfo').show();
					
					var opeString = "<table>";
					for (var k in res.opeinfo) {
						for (var k2 in res.opeinfo[k]) {
							opeString += "<tr>";
							opeString += "<td>" + res.opeinfo[k][k2].custom + "</td>";
							opeString += "<td>" + res.opeinfo[k][k2].preDate + "</td>";
							if (res.opeinfo[k][k2].prid == "0") {
								opeString += "<td><input type='checkbox' name='ppoid[]' value='" + k2 + "' /></td>";
							} else opeString += "<td>已执行</td>";
							opeString += "</tr>";
						}
					}
					
					if (opeString != "<table>") {
						opeString += "</table>";
						jQuery('#opes').html(opeString);
					} else {
						jQuery('#opes').html('').parents('.hideinfo').hide();
						return;
					}					
				}
			});
		}
	});
})
